import { addTaskContent, updateTaskContent, getTaskDetails, taskList } from '@/services/task';

export default {
  namespace: 'task',

  state: {
    taskData: {
      list: []
    }
  },

  effects: {
    // 拉取列表
    *fetch({ payload }, { call, put }) {
      const response = yield call(taskList, payload);
      if (response.code === 0 && response.result) {
        const dataSource = {
          list: response.result.records,
          pagination: {
            total: response.result.total,
            pageSize: response.result.size,
          }
        };
        yield put({
          type: "save",
          payload: dataSource
        });
      }
    },
    *getDetails({ payload,callback }, { call }) {
      const response = yield call(getTaskDetails, payload);
      console.log(response);
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addTaskContent, payload);
      console.log(response);
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateTaskContent, payload);
      console.log(response);
      if (callback) callback(response);
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        taskData: action.payload,
      };
    },
  },
};
